package com.ruoyi.jjkc.cszzc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.jjkc.cszzc.domain.Cszc;
import com.ruoyi.jjkc.cszzc.domain.CszcFj;
import com.ruoyi.jjkc.cszzc.service.ICszcService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 财税政策Controller
 *
 * @author huacai
 * @date 2024-12-27
 */
@RestController
@RequestMapping("/jjkc/cszc")
public class CszcController extends BaseController {
    @Resource
    private ICszcService cszcService;

    /**
     * 查询财税政策列表
     */
    @PreAuthorize("@ss.hasPermi('jjkc:cszc:list')")
    @GetMapping("/list")
    public TableDataInfo list(Cszc cszc) {
        startPage();
        List<Cszc> list = cszcService.selectCszcList(cszc);
        return getDataTable(list);
    }

    /**
     * 导出财税政策列表
     */
    @PreAuthorize("@ss.hasPermi('jjkc:cszc:export')")
    @Log(title = "财税政策", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cszc cszc) {
        List<Cszc> list = cszcService.selectCszcList(cszc);
        ExcelUtil<Cszc> util = new ExcelUtil<Cszc>(Cszc.class);
        util.exportExcel(response, list, "财税政策数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Cszc> util = new ExcelUtil<Cszc>(Cszc.class);
        util.importTemplateExcel(response, "财税政策数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "财税政策", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('jjkc:cszc:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Cszc> util = new ExcelUtil<Cszc>(Cszc.class);
        InputStream inputStream = file.getInputStream();
        List<Cszc> list = util.importExcel(inputStream);
        inputStream.close();
        int count = cszcService.batchInsertCszc(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取财税政策详细信息
     */
    @PreAuthorize("@ss.hasPermi('jjkc:cszc:query')")
    @GetMapping(value = "/{cszcId}")
    public AjaxResult getInfo(@PathVariable("cszcId") String cszcId) {
        return success(cszcService.selectCszcByCszcId(cszcId));
    }

    /**
     * 新增财税政策
     */
    @PreAuthorize("@ss.hasPermi('jjkc:cszc:add')")
    @Log(title = "财税政策", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Cszc cszc) {
        return toAjax(cszcService.insertCszc(cszc));
    }

    /**
     * 修改财税政策
     */
    @PreAuthorize("@ss.hasPermi('jjkc:cszc:edit')")
    @Log(title = "财税政策", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Cszc cszc) {
        return toAjax(cszcService.updateCszc(cszc));
    }

    /**
     * 删除财税政策
     */
    @PreAuthorize("@ss.hasPermi('jjkc:cszc:remove')")
    @Log(title = "财税政策", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cszcIds}")
    public AjaxResult remove(@PathVariable String[] cszcIds) {
        return toAjax(cszcService.deleteCszcByCszcIds(cszcIds));
    }

    /**
     * 获得财税政策主键ID (UUID)
     */
    @GetMapping("/getCsZcId")
    public AjaxResult getCsZcId() {
        String getCsZcId = IdUtils.fastSimpleUUID();
        return success(getCsZcId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertCszcFjByCszcId(@RequestBody CszcFj cszcFj) {
        return toAjax(cszcService.insertCszcFjByCszcId(cszcFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectCszcFjList(String sId) {
        CszcFj cszcFj = new CszcFj();
        cszcFj.setCszcId(sId);
        return AjaxResult.success(cszcService.selectCszcFjList(cszcFj));
    }

    /**
     * 删除财税政策附件
     */
    @Log(title = "财税政策附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteCszcFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(cszcService.deleteCszcFjByFjIds(fjIds));
    }

}
